package crayon.fc;

import java.awt.Point;
import java.util.ArrayList;


public class GestionnaireTracing{
    private ArrayList<Gestionnaire_Point> m_Tlist;
    int IndexOfcurrentArray;
    Point currentPoint;
    Point currentTan;
    public GestionnaireTracing(){
	m_Tlist= new ArrayList<Gestionnaire_Point>();
	NewTracing();
	IndexOfcurrentArray=0;
    }
    
    public void NewTracing(){
	m_Tlist.add(new Gestionnaire_Point());
	IndexOfcurrentArray++;
	currentPoint=null;
	currentTan=null;
    }
        
    public void vider(){
	m_Tlist= new ArrayList<Gestionnaire_Point>();
	NewTracing();
	IndexOfcurrentArray=0;
	System.out.println("vider");
   
    }
    public void delete(){
	m_Tlist.get(IndexOfcurrentArray).delete(currentPoint);
	currentPoint=null;
    }
    
    public ArrayList<Gestionnaire_Point> getTracings(){
	return  m_Tlist;
    }

    public void addPoint(Point p){
	Gestionnaire_Point m_GPoint=m_Tlist.get(IndexOfcurrentArray);
	if(m_GPoint.closePoint1!=null)
	    return ;// si la courbe est fermee pas de possibilite d'ajout	
	//si le point courrant est en bout de chaine alors on autorise l'ajout 
	//si  la chaine etait vide on ajoute 
	if(m_GPoint.m_Plist.size()==0)
	    m_GPoint.m_Plist.add(new SpecializedPoint(p));//en tete    
	else if(m_GPoint.get(m_GPoint.m_Plist.size()-1)==currentPoint)
	    m_GPoint.m_Plist.add(new SpecializedPoint(p));//en tete
	else if(m_GPoint.get(0)==currentPoint)
	    m_GPoint.m_Plist.add(0,new SpecializedPoint(p));//en queue
	else
	    return ; //on ajoute pas le point
	currentPoint=p;//on met a jour le point courant
	currentTan=null;
    }
 
    public void modify(Point newp){
	m_Tlist.get(IndexOfcurrentArray).set(currentPoint,newp);
	currentPoint=newp;
    }
    public void modifyCtrl(Point newp){
	m_Tlist.get(IndexOfcurrentArray).setCtrl(currentPoint,newp);
	currentTan=newp;
    }
    
    public void setPoint(Point p){
	int i=0;
	currentPoint=null;
	for(Gestionnaire_Point gestionnaire_point : m_Tlist){
	    currentPoint=gestionnaire_point.getPoint(p);
	    if(currentPoint!=null){
		IndexOfcurrentArray=i;
		break;
	    }
	    else
		i++;
	}
	if (IndexOfcurrentArray>m_Tlist.size())
	    IndexOfcurrentArray=0;
    }
        
    public void setPointTan(Point ptan){
	int i=0;
	currentPoint=null;
	for(Gestionnaire_Point gestionnaire_point : m_Tlist){
	    currentPoint=gestionnaire_point.getPointTan(ptan);
	    if(currentPoint!=null){
		IndexOfcurrentArray=i;
		break;
	    }
	    else
		i++;
	}
	if (IndexOfcurrentArray>m_Tlist.size())
	    IndexOfcurrentArray=0;
    }    
    
    public boolean onPoint(Point p ){
	for(Gestionnaire_Point gestionnaire_point : m_Tlist)
	    if(gestionnaire_point.InMyList(p))
		return true;
	return false;
    }
    public boolean onTan(Point p ){
	for(Gestionnaire_Point gestionnaire_point : m_Tlist)
	    if(gestionnaire_point.InMyListTan(p))
		return true;
	return false;
    }
    public boolean samePoint(Point p){
	if (currentPoint!=null)
	    if (p.x<=currentPoint.x+Gestionnaire_Point.CubeDemiSize && p.x>=currentPoint.x-Gestionnaire_Point.CubeDemiSize && p.y<=currentPoint.y+Gestionnaire_Point.CubeDemiSize && p.y>=currentPoint.y-Gestionnaire_Point.CubeDemiSize )
	    return true;
	return false;
    }
    
    public Point currentPoint(){
	return currentPoint;
    }
    public Point currentTan(){
	return currentTan;
    }
    
    
    public void unselect(){
	currentPoint=null;
    }
    public void setCloserPATH(Point p){
	m_Tlist.get(IndexOfcurrentArray).setClosePoint(currentPoint,p);
	NewTracing();
    }

    public boolean onPointOfcurrentPath(Point p ){
	if(m_Tlist.get(IndexOfcurrentArray).InMyList(p))
		return true;
	return false;
    }
}